import { Controller, Get, Post, Body, Patch, Param, Delete, Query } from '@nestjs/common';
import { MessageService } from './message.service';
import { CreateMessageDto } from './dto/create-message.dto';
import { UpdateMessageDto } from './dto/update-message.dto';
import { Message } from './entities/message.entity';
import { ApiResponse, ApiOperation, ApiQuery, ApiParam } from '@nestjs/swagger';
import { GetMessagesResponseDto } from './dto/response.dto';

@Controller('message')
export class MessageController {
  constructor(private readonly messageService: MessageService) { }

  @Post()
  create(@Body() createMessageDto: CreateMessageDto) {
    return this.messageService.create(createMessageDto);
  }
  @Get('getHello')
  getHello(): string {
    return this.messageService.getHello();
  }
  //无分页 http://localhost:3000/message/last

  @Get('last')
  async getLastMessages(@Query('device_name') device_name?: string): Promise<Message[]> {
    return this.messageService.getLastMessages(device_name);
  }
  // 有分页 http://localhost:3000/message/updated?page=1&limit=30 
  @ApiOperation({ summary: '获取最新的设备产气记录' })
  @ApiQuery({ name: 'device_name', required: false, description: '设备名称' })
  @ApiQuery({ name: 'limit', required: false, description: '每页显示条数' })
  @ApiQuery({ name: 'page', required: false, description: '页码' })
  @ApiResponse({ status: 200, description: '返回消息列表', type: GetMessagesResponseDto })

  @Get('updated')
  async getMessages(
    @Query('device_name') device_name?: string,
    @Query('page') page?: number,
    @Query('limit') limit?: number,
  ): Promise<{ total: number; page: number; limit: number; list: Message[] }> {
    return this.messageService.getMessages(device_name, page, limit);
  }


  @ApiOperation({ summary: '测试是否可以连接数据库' })
  @Get('test-connection')
  async testDatabaseConnection(): Promise<string> {
    const isConnected = await this.messageService.testConnection();
    return isConnected ? 'Database connection is successful' : 'Database connection failed';
  }
  @Get()
  findAll() {
    return this.messageService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.messageService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateMessageDto: UpdateMessageDto) {
    return this.messageService.update(+id, updateMessageDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.messageService.remove(+id);
  }
}
